package httpapi

import (
	"strconv"

	"away.wei/minidevpos/db"
	"away.wei/minidevpos/model"
	"github.com/gin-gonic/gin"
)

type ProjectApi struct{}

// @Tags Project
// @Summary 添加
// @Security ApiKeyAuth
// @Param data body db.Project  true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /project [post]
func (s *ProjectApi) Create(c *gin.Context) {
	var dto db.Project
	_ = c.ShouldBindJSON(&dto)
	if data, err := projectRep.Create(dto); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithData(data, c)
	}
}

// @Tags Project
// @Summary 删除
// @Security ApiKeyAuth
// @Param ids body []int true "编号"
// @Router /project [delete]
func (s *ProjectApi) Delete(c *gin.Context) {
	var ids []int
	c.ShouldBindJSON(&ids)
	if err := projectRep.Delete(ids); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("删除成功", c)
	}
}

// @Tags Project
// @Summary 更新
// @Security ApiKeyAuth
// @Param data body db.Project  true " "
// @Router /project [put]
func (s *ProjectApi) Update(c *gin.Context) {
	var dto db.Project
	_ = c.ShouldBindJSON(&dto)
	if err := projectRep.Update(dto); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithData(dto, c)
	}
}

// @Tags Project
// @Summary 查详情
// @Security ApiKeyAuth
// @Param id query int true "编号"
// @Router /project/details [get]
func (s *ProjectApi) Details(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	if model, err := projectRep.Details(id); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithData(model, c)
	}
}

// @Tags Project
// @Summary 分页查询
// @Security ApiKeyAuth
// @Param data query model.ProjectSearch true " "
// @Router /project/getpage [get]
func (s *ProjectApi) GetPage(c *gin.Context) {
	var dto model.ProjectSearch
	c.ShouldBindQuery(&dto)
	list, total, err := projectRep.GetPage(dto)

	if err != nil {
		FailWithMessage("获取失败", c)
	} else {
		OkWithDetailed(PageResult{
			List:     list,
			Total:    total,
			Page:     dto.Page,
			PageSize: dto.PageSize,
		}, "获取成功", c)
	}
}
